<?php

class dbutils
{
    static $rest2db = array(
        'idUser' => 'id_user',
        'idJourney' => 'id_journey',
        'idRoute' => 'id_route',
        'idMessage' => 'id_message',
        'journeyStatus' => 'journey_status',
        'departureAddr' => 'departure_addr',
        'departureLat' => 'departure_lat',
        'departureLng' => 'departure_lng',
        'arrivaAddr' => 'arriva_addr',
        'arrivaLat' => 'arriva_lat',
        'arrivaLng' => 'arriva_lng',
        'routeMeters' => 'route_lenght_mt',
        'departureDateTime' => 'departure_datetime',
        'owner' => 'fl_owner',
        'online' => 'fl_on_line',
        'birthYear' => 'birth_year',
        'cdContact' => 'cd_contact',
        'dsContact' => 'ds_contact',
        'idSender' => 'id_sender',
        'idRecipient' => 'id_recipient',
        'cdTongue' => 'cd_tongue'
    );

    static $db2rest = array(
        'id_user' => 'idUser',
        'id_journey' => 'idJourney',
        'id_route' => 'idRoute',
        'id_message' => 'idMessage',
        'journey_status' => 'journeyStatus',
        'departure_addr' => 'departureAddr',
        'departure_lat' => 'departureLat',
        'departure_lng' => 'departureLng',
        'arriva_addr' => 'arrivaAddr',
        'arriva_lat' => 'arrivaLat',
        'arriva_lng' => 'arrivaLng',
        'route_lenght_mt' => 'routeMeters',
        'departure_datetime' => 'departureDateTime',
        'fl_owner' => 'owner',
        'fl_on_line' => 'online',
        'birth_year' => 'birthYear',
        'cd_contact' => 'cdContact',
        'ds_contact' => 'dsContact',
        'id_sender' => 'idSender',
        'id_recipient' => 'idRecipient',
        'cd_tongue' => 'cdTongue',
        'ds_tongue' => 'dsTongue'
    );

    static public $conn_string = "host=postgresql1.alwaysdata.com dbname=mytaxisharing_prova user=mytaxisharing_ospite password=ospite";

//    static public $conn_string = 'host=127.0.0.1 dbname=postgres user=ospite password=qazxswedc';

    public static function translate_db_params($db_param)
    {
        $values = array();
        foreach ($db_param as $key => $val) {
            if (is_array($val))
                $values[$key] = dbutils::translate_db_params($val);
            else {
                if (isset(dbutils::$db2rest[$key]))
                    $values[dbutils::$db2rest[$key]] = $val;
                else
                    $values[$key] = $val;
            }
        }
        return $values;
    }

    public static function translate_rest_params($rest_param)
    {
        $db_handle = @pg_pconnect(dbutils::$conn_string);
        foreach ($rest_param as $key => $val) {
            if (is_array($val))
                $values[$key] = dbutils::translate_rest_params($val);
            else {
                if (isset(dbutils::$rest2db[$key]))
                    $values[dbutils::$rest2db[$key]] = @pg_escape_string($db_handle, $val);
                else
                    $values[$key] = @pg_escape_string($db_handle, $val);
            }
        }
        @pg_close($db_handle);
        return $values;
    }

    public static function dbresult_to_json($result)
    {
        $numrows = @pg_num_rows($result);
        if ($numrows > 0) {
            $jsonData = array();
            for ($row = 0; $row < $numrows; $row++) {
                $jsonData[] = json_encode(dbutils::translate_db_params(@pg_fetch_array($result, $row, PGSQL_ASSOC)));
            }
            return "[" . implode(", ", $jsonData) . "]";
        } else
            return "[]";
    }

    public static function dbresult_to_array($result)
    {
        $numrows = @pg_num_rows($result);
        if ($numrows > 0) {
            $arrayData = array();
            for ($row = 0; $row < $numrows; $row++) {
                $arrayData[] = @pg_fetch_array($result, $row, PGSQL_ASSOC);
            }
            return $arrayData;
        } else
            return array();
    }

    public static function error_to_json($code, $msg)
    {
        return json_encode(array("code" => $code, "message" => $msg));
    }
}

?>